package com.ccys.cms.controller.administrator;

import com.ccys.cms.service.ModuleService;
import com.ccys.common.constants.ContextConstants;
import com.ccys.common.constants.TouristApi;
import com.ccys.common.util.Kit;
import com.ccys.common.vo.ResponseResult;
import com.ccys.db.dto.web.base.ModuleDto;
import com.ccys.db.entity.SysModule;
import com.ccys.db.vo.web.base.ModuleListVo;
import io.swagger.annotations.*;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import static com.ccys.common.util.Kit.*;
import static com.ccys.common.vo.ResponseResult.update;


/**
 * 模块
 */
@RestController
@RequestMapping("/administrator")
@Api(tags = "系统-权限模块")
public class ModuleController {


    @Resource
    private ModuleService moduleService;

    @GetMapping("/module")
    @ApiOperation("获取模块列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pid", value = "父级模块", paramType = "query"),
    })
    @ApiResponses({
            @ApiResponse(code = 200, message = ContextConstants.MODEL_DESC, response = ModuleListVo.class)
    })
    public ResponseResult manage( @ApiIgnore ModuleDto moduleDto) {
        return moduleService.queryModules( moduleDto);
    }

    @PostMapping("/module")
    @ApiOperation("添加模块")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pid", value = "父级模块", paramType = "query"),
            @ApiImplicitParam(name = "name", value = "模块名称", paramType = "query"),
            @ApiImplicitParam(name = "path", value = "请求路径", paramType = "query"),
            @ApiImplicitParam(name = "number", value = "序号", paramType = "query"),
            @ApiImplicitParam(name = "description", value = "描述", paramType = "query"),
    })
    @Transactional
    public ResponseResult manageInsert( @ApiIgnore SysModule module) {
        allParamValid(module, "pid", "name", "path", "number", "description");
        int i = moduleService.insertOrUpdate( module);
        moduleService.setCache();
        return update(i);
    }

    @PutMapping("/module")
    @ApiOperation("修改模块")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "模块id", paramType = "query"),
            @ApiImplicitParam(name = "pid", value = "父级模块", paramType = "query"),
            @ApiImplicitParam(name = "name", value = "模块名称", paramType = "query"),
            @ApiImplicitParam(name = "path", value = "请求路径", paramType = "query"),
            @ApiImplicitParam(name = "number", value = "序号", paramType = "query"),
            @ApiImplicitParam(name = "description", value = "描述", paramType = "query"),
    })
    @Transactional
    public ResponseResult manageUpdate(@ApiIgnore SysModule module) {
        allParamValid(module, "id");
        int i = moduleService.insertOrUpdate( module);
        moduleService.setCache();
        return update(i);
    }

    @DeleteMapping("/module")
    @ApiOperation("删除模块")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "模块id", paramType = "query"),
    })
    public ResponseResult manageDelete( @ApiIgnore SysModule module) {
        allParamValid(module, "id");
        module.setDelFlag(1);
        int i = moduleService.insertOrUpdate(module);
        moduleService.setCache();
        return update(i);
    }

    @TouristApi
    @PostMapping("module/oneClickGeneration")
    @ApiOperation("一键生成模块")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "jsonArray", value = "JSON数组([{\"access\":\"Access\",\"component\":\"Component\",\"icon\":\"Icon\",\"name\":\"系统管理\",\"path\":\"/sys\",\"children\":[{\"access\":\"Access\",\"component\":\"Component\",\"icon\":\"Icon\",\"name\":\"角色管理\",\"path\":\"/sys/role\"}]}])", required = true, paramType = "query"),
    })
    @Transactional
    public ResponseResult oneClickGeneration( String jsonArray) {
        Kit.requisiteParam( "jsonArray");
        return moduleService.oneClickGeneration( jsonArray);
    }

}
